package org.laowenchao.client;




import org.laowenchao.client.model.Folder;
import org.laowenchao.client.rpc.ArticleService;
import org.laowenchao.client.rpc.ArticleServiceAsync;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.RootPanel;



public class ArticleList implements EntryPoint {

	ArticleServiceAsync blogService = GWT.create(ArticleService.class);
	Folder model;
	@Override
	public void onModuleLoad() {
		Window.alert("Start");
		((ServiceDefTarget)blogService).setServiceEntryPoint("/ArticleService");			
		//初始数据异步读入		
		blogService.getFolder(new AsyncCallback<Folder>(){

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());

			}

			@Override
			public void onSuccess(Folder result) {
				Window.alert("Success");
				model = result;

				TreeStore<ModelData> store = new TreeStore<ModelData>();  

				store.add(model.getChildren(), true);  

				final TreePanel<ModelData> tree = new TreePanel<ModelData>(store);  
				tree.setWidth(300);  
				tree.setDisplayProperty("name");  
				tree.getStyle().setLeafIcon(null);  
				//Tree按键响应
				tree.addListener(Events.OnClick, new Listener<TreePanelEvent<ModelData>>(){
					@Override
					public void handleEvent(TreePanelEvent<ModelData> event) {
						Long id = event.getItem().get("id");
						
						if(id<0)
						{
							Window.alert((String) event.getItem().get("name"));							
						}
						else
						{
							//文章信息异步读入
							blogService.getArticleById(id, new AsyncCallback<String[]>(){

								@Override
								public void onFailure(Throwable caught) {
									Window.alert(caught.getMessage());
									
								}

								@Override
								public void onSuccess(String[] result) {
																		
								}
								
							});
						}

						
						
					}});


				RootPanel.get().add(tree);

			}

		});






	}



}
